package com.mcp.converter.domain.rule.conversion.datatype;

import com.mcp.converter.domain.rule.conversion.ConversionRule;
import com.mcp.converter.shared.enums.IncompatibilityType;
import com.mcp.converter.shared.enums.SQLElementType;
import com.mcp.converter.domain.converter.ConversionSuggestion;
import com.mcp.converter.shared.response.sql.SQLElement;

/**
 * LONG数据类型转换规则
 * 将LONG数据类型转换为CLOB数据类型
 */
public class LongToClobConversionRule implements ConversionRule {
    
    private static final String RULE_ID = "CV_DT003";
    private static final String RULE_NAME = "LONG转CLOB";
    private static final String RULE_DESCRIPTION = "将LONG数据类型转换为CLOB数据类型";
    
    private ConversionSuggestion conversionSuggestion;
    
    public LongToClobConversionRule() {
        this.conversionSuggestion = new ConversionSuggestion();
        this.conversionSuggestion.setSuggestion("将LONG数据类型替换为CLOB数据类型");
        this.conversionSuggestion.setExample("CREATE TABLE test (col LONG) -> CREATE TABLE test (col CLOB)");
        this.conversionSuggestion.setAutoConvertible(true);
        this.conversionSuggestion.setComplexity(ConversionSuggestion.Complexity.SIMPLE);
    }
    
    @Override
    public String getId() {
        return RULE_ID;
    }
    
    @Override
    public String getName() {
        return RULE_NAME;
    }
    
    @Override
    public String getDescription() {
        return RULE_DESCRIPTION;
    }
    
    @Override
    public IncompatibilityType getIncompatibilityType() {
        return IncompatibilityType.DATA_TYPE;
    }
    
    @Override
    public ConversionSuggestion getConversionSuggestion() {
        return conversionSuggestion;
    }
    
    @Override
    public boolean matches(Object target) {
        if (target instanceof SQLElement) {
            SQLElement element = (SQLElement) target;
            if (element.getType() == SQLElementType.DATA_TYPE) {
                String dataTypeName = element.getName().toUpperCase();
                return "LONG".equals(dataTypeName) && !"LONG RAW".equals(dataTypeName);
            }
        }
        return false;
    }
    
    @Override
    public String convert(SQLElement element, ConversionContext context) {
        String original = element.getValue() != null ? element.getValue() : "LONG";
        String converted = "CLOB";
        
        context.addConversionDetail(original, converted, "LONG数据类型在OceanBase中不支持，已转换为CLOB");
        
        return converted;
    }
}
